import * as Common from './common.js'
import { Util } from "./util.js";

export class MsgMgr
{
    static url = Common.UrlBase + Common.ApiEnums.MsgMgr;

    constructor()
    {
        // 构造函数  
    }

    static sendMsg(uid, uname, msg, type, cb)
    {
        // 类的方法  
        $.post(
            this.url,
            {
                "action": "msg_add",
                "user_id": uid,
                "user_name": uname,
                "content": msg,
                "type": type
            },
            function (res)
            {
                if (cb)
                {
                    cb();
                }

            }
        );
    }

    static getMsgByType(type)
    {
        $.post(
            this.url,
            {
                "action": "msg_get_by_type",
                "type": type
            },

            function (res)
            {
                var resobj = JSON.parse(res);
                var msgDiv = document.getElementById(Util.getMsgidByType(type) + "-pane");
                if (resobj.code == "200")
                {
                    $(msgDiv).children().remove();

                    resobj.data.forEach(dobj =>
                    {
                        var newnode = $("<p>" + (dobj.timestamp).substring(11, 19) + "&nbsp<span class='badge text-bg-danger'>" + dobj.user_name + "</span>:" + dobj.content + "</p>")
                        $(msgDiv).append(newnode);

                    })

                } else
                {
                    $(msgDiv).children().remove();

                    var newnode = $("<p>无消息</p>")
                    $(msgDiv).prepend(newnode);
                }


            }
        );
    }
}